package com.zhuangjie.order.controller;


import com.zhaungjie.base.conduit.R;
import com.zhuangjie.base.constant.ModuleSecurityNames;
import com.zhuangjie.mvc.annotation.ParamInject;
import com.zhuangjie.order.dto.OrderListForUserPageDTO;
import com.zhuangjie.order.dto.ShopOrderPageDTO;
import com.zhuangjie.order.dto.ShopRejectOrderDTO;
import com.zhuangjie.order.dto.SubmitOrderDTO;
import com.zhuangjie.order.service.OrderService;
import com.zhuangjie.order.vo.CreateOrderCompleteVO;
import com.zhuangjie.order.vo.OrderDetailForDeliveryOrderDetailVO;
import com.zhuangjie.order.vo.OrderForMapShow;
import com.zhuangjie.order.vo.ShopOrderPageVO;
import com.zhuangjie.security.annotations.NoAuthentication;
import com.zhuangjie.security.annotations.OnlySystemCall;
import com.zhuangjie.security.annotations.PublicAPI;
import com.zhuangjie.security.provider.UserProvider;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-11-24
 */
@RestController
@RequestMapping("/order")
@PublicAPI(modules = {ModuleSecurityNames.USER})
public class OrderController {
    @Autowired
    private OrderService orderService;


    @ApiOperation("创建订单")
    @PostMapping("/create")
    // TODO 这里需要做限流 @AccessLimit
    public R createOrder(@Valid @RequestBody SubmitOrderDTO submitOrderDTO) throws Exception {
        return R.ok().data(CreateOrderCompleteVO.class, orderService.createOrder(submitOrderDTO));
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancel/{orderId}")
    public R cancelOrder(@PathVariable("orderId") Long orderId) {
        return R.capture(() -> orderService.cancelOrder(orderId, "用户主动取消"));
    }


    @ApiOperation("模拟接口：支付订单")
    @PostMapping("/payment/{orderId}")
    public R paymentOrder(@NotNull(message = "订单id不能为空") @PathVariable("orderId") Long orderId) {
        return R.capture(() -> orderService.paymentOrder(orderId));
    }

    @ApiOperation("获取订单信息")
    @GetMapping("/info/{orderId}")
    public R orderInfo(@NotNull(message = "订单id不能为空") @PathVariable("orderId") Long orderId) {
        return R.require(orderService.getOrderInfo(orderId));
    }

    @ApiOperation("查询店铺的订单列表")
    @GetMapping("/list-for-shop")
    @PublicAPI(modules = {ModuleSecurityNames.SHOP})
    public R listShopOrder(@Valid @ParamInject ShopOrderPageDTO shopOrderPageDTO) {
        return R.ok().data(ShopOrderPageVO.class, orderService.listShopOrder(shopOrderPageDTO));
    }

    @ApiOperation("商家拒单")
    @PostMapping("/shop-reject-order")
    @PublicAPI(modules = {ModuleSecurityNames.SHOP})
    public R shopRejectOrder(@Valid ShopRejectOrderDTO shopRejectOrderDTO) {
        return R.capture(() -> orderService.shopRejectOrder(shopRejectOrderDTO));
    }

    @ApiOperation("商家接单")
    @PostMapping("/receiving-order/{orderId}")
    @PublicAPI(modules = {ModuleSecurityNames.SHOP})
    public R shopReceivingOrder(@PathVariable("orderId") Long orderId) {
        return R.capture(() -> orderService.shopReceivingOrder(orderId));
    }

    @ApiOperation("制作完成/已端餐")
    @PostMapping("/production-completed/{orderId}")
    @PublicAPI(modules = {ModuleSecurityNames.SHOP})
    public R productionCompleted(@PathVariable("orderId") Long orderId) {
        return R.capture(() -> orderService.productionCompleted(orderId));
    }

    @ApiOperation("查询用户的订单列表")
    @GetMapping("/list-for-user")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R getOrderListForUser(@Valid @ParamInject OrderListForUserPageDTO orderListForUserPageDTO) throws Exception {
        return R.ok().data(orderService.getOrderListForUser(orderListForUserPageDTO));
    }

    @ApiOperation("获取订单中的菜品")
    @GetMapping("/dishes")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    private R listDishes(@NotNull(message = "订单id不能为空") @RequestParam("orderId") Long orderId) {
        return R.require(orderService.listDisheForEvaluateListByOrderId(orderId));
    }

    @ApiOperation("获取订单中的菜品")
    @GetMapping("/order-detail-for-delivery-order-detail")
    @PublicAPI(modules = {ModuleSecurityNames.RIDER})
    private R orderDetailForDeliveryOrderDetail(@NotNull(message = "订单id不能为空") @RequestParam("orderId") Long orderId) {
        return R.require(OrderDetailForDeliveryOrderDetailVO.class, orderService.orderDetailForDeliveryOrderDetail(orderId));
    }

    @ApiOperation("修改配送订单为派送中-内部使用")
    @PutMapping("/{orderId}/delivering")
    @PublicAPI(modules = {ModuleSecurityNames.RIDER})
    @OnlySystemCall
    private R updateDeliveryOrderToDelivering(@NotNull(message = "订单id不能为空") @PathVariable("orderId") Long orderId) {
        return R.capture(() -> orderService.updateDeliveryOrderToDelivering(orderId));
    }

    @ApiOperation("修改配送订单为已完成-内部使用")
    @PutMapping("/{orderId}/completed")
    @PublicAPI(modules = {ModuleSecurityNames.RIDER})
    @OnlySystemCall
    private R updateDeliveryOrderToCompleted(@NotNull(message = "订单id不能为空") @PathVariable("orderId") Long orderId) {
        return R.capture(() -> orderService.updateDeliveryOrderToCompleted(orderId));
    }

    @ApiOperation("是否新客")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    @GetMapping("/is-new-customer/{shopId}")
    public R isNewCustomer(@PathVariable("shopId") Long shopId) {
        return R.require(orderService.isNewCustomer(shopId, UserProvider.currentUserId()));
    }

    /**
     * 获取用户的订单信息-进行地图展示
     */
    @ApiOperation("获取用户的订单信息-进行地图展示")
    @GetMapping("/order-for-map-show")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R orderForMapShow(@RequestParam(value = "orderId", required = false) Long orderId) throws Exception {
        return R.ok().data(OrderForMapShow.class, orderService.getOneMapShowOrderInfo(orderId));
    }

    @ApiOperation("获取用户MapForOrder的订单列表")
    @GetMapping("/list-for-map-show")
    @PublicAPI(modules = {ModuleSecurityNames.USER})
    public R listForMapShow()  {
        return R.ok().data(orderService.listForMapShow());
    }

    @ApiOperation("查询指定shopIds近30天月售数量")
    @GetMapping("/month-sales-count")
    @NoAuthentication
    public R batchGetShopMonthSalesCount(@NotBlank(message = "shopIds不能为空") @RequestParam("shopIds") String shopIds, @RequestParam("recentDays") Integer recentDays) {
        return R.require(orderService.monthSalesCount( Arrays.stream(shopIds.split(",")).map(Long::valueOf).collect(Collectors.toList()), recentDays));
    }




}

